package com.yiwyn.Behavioral.cor;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * COR demo
 * 责任链模式 demo
 */
public class CorDemo {


    public static void main(String[] args) {

        // yiwyn要休假
        PersonLeaveApply apply = new PersonLeaveApply("Yiwyn", 4);

        leaveApply(apply);
    }

    public static void leaveApply(PersonLeaveApply apply) {

        SupervisorsApprover supervisorsApprover = new SupervisorsApprover();
        supervisorsApprover.setNextHandle(new ManagerApprover());

        supervisorsApprover.approve(apply);

    }


    // 休假申请
    @Data
    @AllArgsConstructor
    public static class PersonLeaveApply {
        // 名字
        private String name;
        // 休假日期
        private int leaveDays;
    }


    // 审批行为
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public abstract static class ApproverHandle {
        // 下一个节点
        private ApproverHandle nextHandle;

        // 同意天数
        public abstract Integer agreeDay();

        // 审批
        public void approve(PersonLeaveApply apply) {
            // 如果当前节点通过，则进入下一个节点
            if (apply.getLeaveDays() <= agreeDay()) {
                if (nextHandle != null) {
                    nextHandle.approve(apply);
                }
            } else {
                throw new RuntimeException(apply.getName() + "请假时间" + apply.getLeaveDays() + "不能被允许! " + this.getClass().getSimpleName() + "审批时间:" + agreeDay());
            }
        }
    }


    // 主管审批
    @Data
    @EqualsAndHashCode(callSuper = true)
    @AllArgsConstructor
    public static class SupervisorsApprover extends ApproverHandle {

        @Override
        public Integer agreeDay() {
            return 4;
        }
    }

    // 经理审批
    @Data
    @EqualsAndHashCode(callSuper = true)
    @AllArgsConstructor
    public static class ManagerApprover extends ApproverHandle {

        @Override
        public Integer agreeDay() {
            return 2;
        }
    }


}
